/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package csvloader;

import Javabeans.Attribute;
import Javabeans.DatPoint;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import Javabeans.DataPoint;
import Javabeans.DataSet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author Muhammed
 * 
 */
public class CSVLoader {
    private CSVReader csvreader;
    private String path;
    private String filename;

    public CSVLoader(String path, String filename) throws FileNotFoundException{
        this.path=path;
        this.filename=filename;
        File f = new File(path+filename);
        FileReader fr = new FileReader(f);
        csvreader = new CSVReader(fr);
    }

    public String[] getAttributes() throws IOException{
        return csvreader.readCSVLine();
    }
    public DataSet getDataSet() throws IOException{
        DataSet dataset = new  DataSet();
        
        String[] header = csvreader.readCSVLine();

        ArrayList<Attribute> attributes = new ArrayList<Attribute>();
        for(String s:header){
            Attribute att = new Attribute(s);
            HashMap matrix =  getSimilarityMatrix(path,s+".csv");
            if(matrix==null){
                att.setType(Attribute.NUMERIC);
            }else{
                att.setType(Attribute.NOMINAL);
            }
            //System.out.println("\tSimilarityMatrix: "+matrix);
            att.setSimilarityMatrix(matrix);
            attributes.add(att);
        }
        dataset.setAttributes(attributes);
        /*
        System.out.print("Header: ");
        for(String s:header){
            System.out.print(s+",");
        }
        System.out.print("\n");
        */
        String[] row;
        int count =1;
        while((row=csvreader.readCSVLine())!=null){
            /*
            System.out.print("Row #"+count+": ");
            for(String s:row){
                System.out.print(s+",");
            }
            System.out.print("\n");
             * 
             */
            
             
            DatPoint p = new DatPoint(row);
            dataset.getDataPoints().add(p);
            count++;
        }
       // System.out.println("DataSet: Number of records loaded: "+count);
        return dataset;
    }
    public HashMap<String, Double> getSimilarityMatrix(String path, String filename) throws IOException{
        HashMap<String, Double> matrix = new HashMap<String, Double>();
        try{
            //System.out.println(path+filename);
            File f = new File(path+filename);
            FileReader fr = new FileReader(f);
            CSVReader tempcsvreader = new CSVReader(fr);

            String[] row;
            int count =1;
            while((row=tempcsvreader.readCSVLine())!=null){
                matrix.put(row[0], Double.parseDouble(row[1]));
                count++;
            }
    //        System.out.println("SimilarityMatrix for "+filename+" Number of records loaded: "+count);
        }catch(FileNotFoundException e){
            return null;
        }
        return matrix;
    }
}


